package com.tuanbiao.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import com.abiao.lib_utils.ActivityUtils;
import com.abiao.lib_utils.SPUtils;
import com.hzjq.lib.BlastKool;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.tuanbiao.App;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.constants.UserConstants;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.databinding.ActivitySettingDeviceBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.MenuDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.events.EncodingRulesEvent;
import com.tuanbiao.models.SettingModel;
import com.tuanbiao.presenter.setting.SettingDevicePresenter;
import com.tuanbiao.ui.EncodingRulesActivity;
import com.tuanbiao.ui.account.LoginActivity;
import com.tuanbiao.ui.MainActivity;
import com.tuanbiao.utils.ClickUtil;
import com.tuanbiao.utils.SPHelper;

import org.greenrobot.eventbus.EventBus;

public class SettingDeviceActivity extends BasePresenterActivity<SettingDevicePresenter, ActivitySettingDeviceBinding> implements SettingController.DeviceView {
    private String mDeviceNum;
    private int[] models = {R.string.setting_more_model_normal, R.string.setting_more_model_alone};
    public static void start(Context context) {
        Intent starter = new Intent(context, SettingDeviceActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivitySettingDeviceBinding getBindingView() {
        return ActivitySettingDeviceBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.sbChangeNum.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            SettingDeviceNumActivity.start(getActivity());
        });
        binding.sbChangeServer.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            SettingServiceActivity.start(getActivity());
        });
        binding.sbChangeZbServer.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            SettingZbServerActivity.start(getActivity());
        });
        binding.sbChangePanel.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            SettingControlPanelActivity.start(getActivity());
        });
        binding.sbModel.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showModelDialog();
        });
        binding.sbEncodingRules.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            EventBus.getDefault().postSticky(new EncodingRulesEvent(true));
            EncodingRulesActivity.start(getActivity());
        });
        binding.sbClear.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showClearDialog();
        });
    }


    private void showClearDialog(){
       new XPopup.Builder(this)
               .asConfirm("提示", "是否清除所有操作数据？","取消","清除后重启", () -> {
                   SPHelper.clearCommit();
                   //ActivityUtils.finishAllActivities();
                   BlastKool.Companion.loader().clearOperateInfo(() -> {
                       App.exitRestart(this);
                   });
               },()->{},false).show();
    }


    @Override
    protected SettingDevicePresenter createPresenter() {
        return new SettingDevicePresenter(new SettingModel(), this);
    }


    @Override
    public void showDeviceNum(String num) {
        mDeviceNum = num;
    }

    @Override
    public void showModel(boolean isStandAloneModel) {
        binding.sbModel.setRightText(isStandAloneModel ? R.string.setting_more_model_alone : R.string.setting_more_model_normal);
    }

    @Override
    public void startLogin() {
        LoginActivity.start(getActivity());
    }

    @Override
    public void startMain() {
        MainActivity.start(getActivity());
    }

    @Override
    public void showType(@UserConstants.KeyEventUserType int type) {
        switch (type) {
            case UserConstants.Type.DEVELOP:
                binding.sbChangePanel.setVisibility(View.VISIBLE);
                binding.sbChangeServer.setVisibility(View.VISIBLE);
                binding.sbChangeZbServer.setVisibility(View.VISIBLE);
                binding.sbChangeNum.setVisibility(View.VISIBLE);
                binding.sbModel.setVisibility(View.VISIBLE);
                binding.sbEncodingRules.setVisibility(View.VISIBLE);
                binding.sbClear.setVisibility(View.VISIBLE);
                break;
            case UserConstants.Type.NORMAL:
                binding.sbChangePanel.setVisibility(View.GONE);
                binding.sbChangeServer.setVisibility(View.VISIBLE);
                binding.sbChangeZbServer.setVisibility(View.VISIBLE);
                binding.sbChangeNum.setVisibility(View.VISIBLE);
                binding.sbModel.setVisibility(View.GONE);
                binding.sbEncodingRules.setVisibility(View.GONE);
                binding.sbClear.setVisibility(View.GONE);
                break;
            case UserConstants.Type.TEST:
                binding.sbChangePanel.setVisibility(View.VISIBLE);
                binding.sbChangeServer.setVisibility(View.VISIBLE);
                binding.sbChangeZbServer.setVisibility(View.VISIBLE);
                binding.sbChangeNum.setVisibility(View.VISIBLE);
                binding.sbModel.setVisibility(View.VISIBLE);
                binding.sbEncodingRules.setVisibility(View.VISIBLE);
                binding.sbClear.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    private void showModelDialog() {
        // 底部选择框
        new MenuDialog.Builder(this)
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(models)
                .setListener((MenuDialog.OnListener<String>) (dialog, position, string) -> {
                    showModelNoteDialog(position != 0);
                })
                .setGravity(Gravity.BOTTOM)
                .setAnimStyle(BaseDialog.ANIM_BOTTOM)
                .show();
    }

    private void showModelNoteDialog(boolean isStandAloneModel) {
        new MessageDialog.Builder(getActivity())
                .setTitle("提示")
                .setMessage("是否永久保留当前选择模式？")
                .setCancel("仅一次")
                .setConfirm("永久")
                .setListener(new MessageDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog) {
                        mPresenter.setModel(true, isStandAloneModel);
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        mPresenter.setModel(false, isStandAloneModel);
                    }
                }).show();
    }
}